#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
int n, m;
string s;
int cnt[128], cnt1[128], cnt2[128];
void solve() {
  cin >> s;
  int l = 0, r = s.size() - 1;
  while (l <= r && s[l] == s[r]) l++, r--;
  if (l > r) {
    cout << 0 << endl;
    return;
  }
  rep(i, 'a', 'z') cnt[i] = cnt1[i] = cnt2[i] = 0;
  rep(i, l, r) cnt[s[i]]++;
  rep(i, 'a', 'z') cnt[i] >>= 1;
  int len1 = 0, len2 = 0;
  rep(i, l, r) {
    ++cnt1[s[i]];
    if (cnt1[s[i]] > cnt[s[i]]) {
      len1 = i - l;
      break;
    }
  }
  per(i, r, l) {
    ++cnt2[s[i]];
    if (cnt2[s[i]] > cnt[s[i]]) {
      len2 = r - i;
      break;
    }
  }
  int len3 = 0;
  if (max(len1, len2) * 2 == r + 1 - l) {
    int mid = (l + r) >> 1;
    int x = mid, y = mid + 1;
    while (s[x] == s[y]) x--, y++, len3++;
  }
  cout << r + 1 - l - max(len1, len2) - len3 << endl;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}